﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using Ccit.Utils;

namespace Ccit.OA.NewsCenter
{
    public partial class news_submit_list : Ccit.Utils.Page
    {
        LoginUser loginUser = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //信息类型
                hdType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";
                loginUser = (LoginUser)Session["LogUserInfo"];
                InitDDL();
            }
            BindGrid();
        }

        private void InitDDL()
        {
            CtrInterface.BindDropDown(ref ddlShortcutClause, "NEWS_A8", "NEWS_A8_10", "NEWS_A8_40", true, "--请选择--", "NEWS_A8_9994='" + loginUser.UserId + "' AND  NEWS_A8_60='1' AND NEWS_A8_30 = '" + hdType.Value + "'");
        }

        private void BindGrid()
        {
            string sClauseXml = "";
            string sType=hdType.Value;
            SubmitBLL oSubmit = new SubmitBLL();
            string sColumnNo="";
            if (Session["QryClause"] == null || Session["QryClause"].ToString() == "")
            {
                string sShortcutClause = ddlShortcutClause.SelectedValue;
                if(sShortcutClause=="")
                {
                    ColumnBLL oColumn = new ColumnBLL();
                    List<MsColumn> lstColumn = oColumn.GetColPub(sType);
                    for (int i = 0; i < lstColumn.Count; i++)
                    {
                        MsColumn msColumn = lstColumn[i];
                        if (sColumnNo == "")
                            sColumnNo = msColumn.columnId;
                        else
                            sColumnNo += "," + msColumn.columnId;
                    }
                }
                else
                    sClauseXml = SysInterface.GetNameStrByIdStr("NEWS_A8", "NEWS_A8_10", "NEWS_A8_50", sShortcutClause, "");
            }
            else
            {
                sClauseXml = Session["QryClause"].ToString();
                Session.Remove("QryClause");
            }
            MsClauseAttr msClauseAttr = new MsClauseAttr(sClauseXml);
            List<MsNews> lstNews = oSubmit.GetNewsByClause(msClauseAttr, sColumnNo, hdType.Value);
            gvNewsList.DataSource = lstNews;
            gvNewsList.DataBind();
        }

        protected void gvNewsList_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;

            string sNewsId = e.KeyValue.ToString();
            string sColumnNo = gvNewsList.GetRowValues(e.VisibleIndex, "ColumnNo").ToString();
            string sNewsTitle = gvNewsList.GetRowValues(e.VisibleIndex, "NewsTitle").ToString();
            string sNewsStatus = gvNewsList.GetRowValues(e.VisibleIndex, "NewsStatus").ToString();
            string sPicFlag = gvNewsList.GetRowValues(e.VisibleIndex, "PicFlag").ToString();
            string sTopFlag = gvNewsList.GetRowValues(e.VisibleIndex, "TopFlag").ToString();
            string sViewCount = gvNewsList.GetRowValues(e.VisibleIndex, "ViewCount").ToString();
            //string sPicId = gvNewsList.GetRowValues(e.VisibleIndex, "PicId").ToString();

            e.Row.Cells[1].Text = "<a href=\"javascript:doViewNews('" + sNewsId + "')\">" + sNewsTitle;
            string sColumnName=SysInterface.GetNameStrByIdStr("NEWS_A2", "NEWS_A2_10", "NEWS_A2_20", sColumnNo, "");
            e.Row.Cells[2].Text = (sColumnName == "") ? "&nbsp;" : sColumnName;

            if (sNewsStatus == "0")
                e.Row.Cells[3].Text = "未提交";
            else if (sNewsStatus == "1")
                e.Row.Cells[3].Text = "已提交";
            else if (sNewsStatus == "2")
                e.Row.Cells[3].Text = "已发布";

            if (sNewsStatus == "0")
            {
                if (sPicFlag == "0")
                    e.Row.Cells[4].Text = "<a href=\"javascript:doChangePic('" + sNewsId + "')\">否";
                else if (sPicFlag == "1")
                    e.Row.Cells[4].Text = "<a href=\"javascript:doChangePic('" + sNewsId + "')\">是";

                if (sTopFlag == "0")
                    e.Row.Cells[5].Text = "<a href=\"javascript:doChangeTopFlag('" + sNewsId + "','0')\">否";
                else if (sTopFlag == "1")
                    e.Row.Cells[5].Text = "<a href=\"javascript:doChangeTopFlag('" + sNewsId + "','1')\">是";
            }
            else
            {
                if (sPicFlag == "0")
                    e.Row.Cells[4].Text = "<a disabled='false'>否";
                else if (sPicFlag == "1")
                    e.Row.Cells[4].Text = "<a disabled='false'>是";

                if (sTopFlag == "0")
                    e.Row.Cells[5].Text = "<a disabled='false'>否";
                else if (sTopFlag == "1")
                    e.Row.Cells[5].Text = "<a disabled='false'>是";
            }

            e.Row.Cells[6].Text = "<a href=\"javascript:doShowViewInfo('" + sNewsId + "')\">" + sViewCount;

            HtmlAnchor haModify = (HtmlAnchor)gvNewsList.FindRowCellTemplateControl(e.VisibleIndex, null, "haModify");
            HtmlAnchor haDelete = (HtmlAnchor)gvNewsList.FindRowCellTemplateControl(e.VisibleIndex, null, "haDelete");
            if (sNewsStatus == "0")
            {
                haModify.Attributes["href"] = "javascript:doModify('" + sNewsId + "')";
                haDelete.Attributes["href"] = "javascript:doDelete('" + sNewsId + "')";
            }
            else
            {
                haModify.Attributes["disabled"] = "false";
                haDelete.Attributes["disabled"] = "false";
                DevExpress.Web.ASPxClasses.Internal.InternalWebControl check = (DevExpress.Web.ASPxClasses.Internal.InternalWebControl)e.Row.Cells[0].Controls[0];
                check.Attributes["disabled"] = "false";
            }
        }

        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            DevExpress.Web.Data.WebDataSelection oSelection = gvNewsList.Selection;
            oSelection.UnselectAll();
            BindGrid();
        }

        /// <summary>
        /// 操作信息
        /// </summary>
        /// <param name="sNewsIds"></param>
        /// <param name="sOperType"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string OperateNews(string sNewsIds, string sOperType,string sCurrTopFlag,string sType)
        {
            bool bFlag = false;
            SubmitBLL oSubmit = new SubmitBLL();
            if (sOperType.Equals("DELETE"))
            {
                bFlag = oSubmit.DeleteNewsByIds(sNewsIds, sType);
            }
            else if (sOperType.Equals("SUBMIT"))
            {
                bFlag = oSubmit.SubmitNewsByIds(sNewsIds);
            }
            else if (sOperType.Equals("TOPFLAG"))
            {
                bFlag = oSubmit.ChangeTopFlag(sNewsIds, sCurrTopFlag);
            }
            var oResult = new { ExeResult = bFlag, OperType = sOperType };
            return JavaScriptConvert.SerializeObject(oResult);
        }
    }
}
